package com.sunyard.pcc.core.service.impl;

import com.sunyard.pcc.common.bo.SysLogBO;
import com.sunyard.pcc.common.bo.SysLogQueryBO;
import com.sunyard.pcc.common.validator.ValidationUtils;
import com.sunyard.pcc.core.service.LogService;
import com.sunyard.pcc.dal.mapper.ext.SysLogExtMapper;
import com.sunyard.pcc.dal.query.SysLogQuery;
import com.sunyard.pcc.dal.query.SysMenuQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Hewei
 * @Description:
 * @Date: Created in 17:00 2018/4/12
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class LogServiceImpl implements LogService {


    @Autowired
    private SysLogExtMapper sysLogExtMapper;

    /**
     * 条件查询日志记录
     * @param sysLogQueryBO
     * @return
     */
    @Override
    public List<SysLogBO> getLogListByCondition(SysLogQueryBO sysLogQueryBO) {
        //非空校验
        ValidationUtils.isBlank(sysLogQueryBO);

        Map<String, Object> condition = getConditionMap(sysLogQueryBO, true);

        List<SysLogBO> logBOS = sysLogExtMapper.getLogListByMap(condition);

        return logBOS;

    }


    /**
     * 条件查询日志条数
     * @param sysLogQueryBO
     * @return
     */
    @Override
    public Integer getLogCountByCondition(SysLogQueryBO sysLogQueryBO) {
        //非空校验
        ValidationUtils.isBlank(sysLogQueryBO);

        Map<String, Object> condition = getConditionMap(sysLogQueryBO, false);

        Integer count = sysLogExtMapper.getLogCountByMap(condition);

        return count;
    }

    /**
     * 查询条件删除
     * @param sysLogQueryBO
     * @return
     */
    private Map<String, Object> getConditionMap(SysLogQueryBO sysLogQueryBO, Boolean pageOffset) {

        Map<String, Object> query = new HashMap<>(64);

        query.put("userName", sysLogQueryBO.getUsername());
        query.put("operation", sysLogQueryBO.getOperation());

        if (pageOffset) {
            Integer pageSize = sysLogQueryBO.getPageSize();
            Integer pageNo = sysLogQueryBO.getPageNo();
            query.put("pageSize", pageSize);
            if ( pageNo != null && pageSize != null){
                //计算页偏移
                query.put("pageOffset", (pageNo - 1) * pageSize);
            }

        }
        return query;

    }
}
